using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Threading;
using Av;
using Av.Utils;
using System.Configuration;
using System.IO;

namespace WebUtils
{
    class Settings
    {

        #region " Name / Version "
        public static string Name = "iWebUtils";
        public static string Version = "0.9.4";
        public static string NameVersion
        {
            get { return string.Format("{0} (v{1})", Name, Version); }
        }
        #endregion

        public class GoogleApp
        {
            public static string ApplicationName { get { return "Test app for google"; } }
        }

        public class Path
        {
            public static string LastOpenedDirectory
            {
                get { return ConfigurationManager.AppSettings["LastOpenedDirectory"]; }

                set
                {
                    // No need to overwrite the same :)
                    if( LastOpenedDirectory == value )
                    {
                        return;
                    }

                    Log4cs.Log(Importance.Debug, "Looking file: {0}", Common.GetPath() + "WebUtils.config");
                    string s = File.ReadAllText(Common.GetPath(true) + "WebUtils.config");
                    int start = s.IndexOf("key=\"LastOpenedDirectory\" value=\"");
                    if( start > 0 )
                    {
                        Log4cs.Log(Importance.Debug, "Index of element is: {0}", start);
                        int end = s.IndexOf("\"", start + 33);
                        if( end > 0 )
                        {
                            Log4cs.Log(Importance.Debug, "End of element is: {0}", end);
                            s = s.Replace(s.Substring(start, end - start), "key=\"LastOpenedDirectory\" value=\"" + value);
                            //Log4cs.Log("New config file:{0}{1}", Environment.NewLine, s);
                            Log4cs.Log("Overwriting configuration file...");
                            File.WriteAllText(Common.GetPath(true) + "WebUtils.config", s);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Events which need attention in whole program, i.e. Exit
        /// </summary>
        public class Events
        {
            /// <summary>
            /// How many events are in list
            /// </summary>
            public const int Count = 1;

            /// <summary>
            /// Named ID of events, to help manage them
            /// </summary>
            public class EventId
            {
                public const int Exit = 0;
            }
            //public enum EventId { Exit = 0 };

            /// <summary>
            /// Array of events which could be raised
            /// </summary>
            public static ManualResetEvent[] ArEvents = null;

            static Events()
            {
                ArEvents = new ManualResetEvent[Events.Count];
                ArEvents[EventId.Exit] = new ManualResetEvent(false);
            }

            /// <summary>
            /// Converts ID of event to string name
            /// </summary>
            public static string IdToName(int id)
            {
                switch(id)
                {
                    case EventId.Exit:
                        return "Exit";
                }

                return id.ToString();
            }
        }

        public static void Load()
        {
            Log4cs.Log("Loading settings...");
        }

        public static string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("Settings for {0} (v{1})", Name, Version);
            sb.AppendLine();

            return sb.ToString();
        }

    }
}
